package ylh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import ylh.common.BaseContext;
import ylh.common.R;
import ylh.dto.OrderDto;
import ylh.entity.*;
import ylh.service.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private OrderDetailService orderDetailService;
    
    @Autowired
    private DishService dishService;
    
    @Autowired
    private SetmealService setmealService;
    
    @Autowired
    private ShoppingCartService shoppingCartService;
    
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, Long number,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        log.info("page = {},pageSize = {},number = {}, beginTime = {}, endTime = {}" ,page,pageSize,number,beginTime,endTime);
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number!=null,Orders::getNumber,number);
        queryWrapper.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.le(endTime!=null,Orders::getOrderTime,endTime);
                
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    @GetMapping("/userPage")
    public R<Page> userPage(Integer page, Integer pageSize){
        log.info("page = {},pageSize = {}" ,page,pageSize);
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrderDto> orderDtoPage = new Page<>();
        
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        
        orderService.page(pageInfo, queryWrapper);

        BeanUtils.copyProperties(pageInfo,orderDtoPage,"records");
        
        List<Orders> orders = pageInfo.getRecords();
        List<OrderDto> orderDtos = orders.stream().map((item)->{
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(item,orderDto);
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderDetail::getOrderId,item.getId());
            List<OrderDetail> orderDetails = orderDetailService.list(lambdaQueryWrapper);
            orderDto.setOrderDetails(orderDetails);
            return orderDto;
        }).collect(Collectors.toList());
        orderDtoPage.setRecords(orderDtos);
        return R.success(orderDtoPage);
    }
    
    @PostMapping("/submit")
    public R<String> orderSubmit(@RequestBody Orders orders){
        
        orderService.submit(orders);
        
        return R.success("提交成功");
    }
    
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("成功");
    }
    
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        Long userId = BaseContext.getCurrentId();
        //获取订单中的菜品
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper );
        
        orderDetails.stream().forEach((item)->{
            ShoppingCart shoppingCart = new ShoppingCart();
            //判断菜品是否还在销售
            if (item.getDishId()!=null){
                LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Dish::getId,item.getDishId());
                lambdaQueryWrapper.eq(Dish::getStatus,1);
                Dish dish = dishService.getOne(lambdaQueryWrapper);
                if (dish == null){
                    return ;
                }
            }else {
                LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Setmeal::getId,item.getSetmealId());
                lambdaQueryWrapper.eq(Setmeal::getStatus,1);
                Setmeal setmeal = setmealService.getOne(lambdaQueryWrapper);
                if (setmeal == null){
                    return ;
                }
            }
            BeanUtils.copyProperties(item,shoppingCart,"id");
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        });
        
        return R.success("添加成功");
    }
}
